<!DOCTYPE html>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象属性提取器</title>
    <link rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-dark.min.css">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: flex-start;
            min-height: 100vh;
            margin: 0;
            background-color: #f0f2f5;
            color: #333;
            padding: 20px;
            box-sizing: border-box;
        }

        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 900px;
            display: flex;
            flex-direction: column;
            gap: 25px;
        }

        h1 {
            color: #0056b3;
            text-align: center;
            margin-bottom: 20px;
            font-size: 2em;
            font-weight: 600;
        }

        .input-group {
            display: flex;
            flex-direction: column;
            margin-bottom: 15px;
        }

        label {
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
            font-size: 0.95em;
        }

        textarea,
        input[type="text"] {
            padding: 12px 15px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 1em;
            width: calc(100% - 30px);
            box-sizing: border-box;
            transition: border-color 0.3s, box-shadow 0.3s;
        }

        textarea:focus,
        input[type="text"]:focus {
            border-color: #007bff;
            box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
            outline: none;
        }

        textarea {
            min-height: 120px;
            resize: vertical;
            font-family: 'Consolas', 'Monaco', monospace;
        }

        .button-group {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 20px;
        }

        button {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 1.05em;
            font-weight: 600;
            transition: background-color 0.3s, transform 0.2s, box-shadow 0.2s;
            color: #fff;
        }

        button.generate {
            background-color: #28a745;
        }

        button.generate:hover {
            background-color: #218838;
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(40, 167, 69, 0.3);
        }

        button.reset {
            background-color: #dc3545;
        }

        button.reset:hover {
            background-color: #c82333;
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(220, 53, 69, 0.3);
        }

        .result-section {
            background-color: #e9ecef;
            padding: 20px;
            border-radius: 8px;
            border: 1px solid #dee2e6;
        }

        .result-section h2 {
            color: #0056b3;
            margin-top: 0;
            font-size: 1.4em;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
            margin-bottom: 15px;
        }

        #resultOutput {
            white-space: pre-wrap;
            word-break: break-all;
            background-color: #f8f9fa;
            border: 1px solid #ced4da;
            padding: 15px;
            border-radius: 6px;
            min-height: 80px;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.95em;
            color: #333;
            overflow-x: auto;
        }

        .error-message {
            color: #dc3545;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 10px;
            border-radius: 6px;
            margin-top: 10px;
            display: none;
            font-size: 0.9em;
        }

        .code-display {
            background-color: #272b33;
            color: #f8f8f2;
            padding: 20px;
            border-radius: 8px;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.9em;
            overflow-x: auto;
            margin-top: 25px;
            box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.2);
        }

        .code-display h2 {
            color: #61dafb;
            margin-top: 0;
            margin-bottom: 15px;
            font-size: 1.4em;
            border-bottom: 2px solid #61dafb;
            padding-bottom: 10px;
        }

        .code-display pre {
            margin: 0;
        }

        .code-display code {
            display: block;
            white-space: pre-wrap;
            word-break: break-all;
        }

        @media (max-width: 768px) {
            .container {
                padding: 20px;
            }

            .button-group {
                flex-direction: column;
            }

            button {
                width: 100%;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>对象属性提取器</h1>

        <div class="input-group">
            <label for="jsonInput">输入 JSON 对象:</label>
            <textarea id="jsonInput"
                placeholder='例如: { "user": { "profile": { "name": "Alice" } }, "data": [1, 2, { "id": 10 }] }'></textarea>
        </div>

        <div class="input-group">
            <label for="selectorInput">输入选择器路径 (多个路径用逗号分隔):</label>
            <input type="text" id="selectorInput" placeholder='例如: user.profile.name, data[2].id, data[0]'>
        </div>

        <div class="button-group">
            <button class="generate" id="generateBtn">提取属性</button>
            <button class="reset" id="resetBtn">重置</button>
        </div>

        <div class="error-message" id="errorMessage"></div>

        <div class="result-section">
            <h2>提取结果</h2>
            <pre id="resultOutput"></pre>
        </div>

        <div class="code-display">
            <h2>`get` 函数核心代码</h2>
            <pre><code id="codeSnippet"  class="language-javascript"></code></pre>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <script>
        // 核心 get 函数
        const get = (from, ...selectors) =>
            [...selectors]
                .filter(s => typeof s === 'string')
                .map(s =>
                    s
                        .replace(/\[([^\[\]]*)\]/g, '.$1.')
                        .split('.')
                        .filter(t => t !== '')
                        .reduce((prev, cur) => prev && prev[cur], from)
                );

        // DOM 元素获取
        const jsonInput = document.getElementById('jsonInput');
        const selectorInput = document.getElementById('selectorInput');
        const generateBtn = document.getElementById('generateBtn');
        const resetBtn = document.getElementById('resetBtn');
        const resultOutput = document.getElementById('resultOutput');
        const errorMessage = document.getElementById('errorMessage');
        const codeSnippet = document.getElementById('codeSnippet');

        // 显示核心代码
        codeSnippet.textContent = `const get = (from, ...selectors) =>
    [...selectors]
        .filter(s => typeof s === 'string')
        .map(s =>
            s
                .replace(/\\[([^\\[\\]]*)\\]/g, '.$1.')
                .split('.')
                .filter(t => t !== '')
                .reduce((prev, cur) => prev && prev[cur], from)
        );`;

        hljs.highlightAll();

        // 事件监听器：提取属性
        generateBtn.addEventListener('click', () => {
            errorMessage.style.display = 'none';
            resultOutput.textContent = '';

            let jsonObj;
            try {
                jsonObj = JSON.parse(jsonInput.value);
            } catch (e) {
                errorMessage.textContent = '错误: 无效的 JSON 输入。请检查语法。';
                errorMessage.style.display = 'block';
                return;
            }

            const selectors = selectorInput.value.split(',').map(s => s.trim()).filter(s => s !== '');

            if (selectors.length === 0) {
                errorMessage.textContent = '错误: 请输入至少一个选择器路径。';
                errorMessage.style.display = 'block';
                return;
            }

            try {
                const results = get(jsonObj, ...selectors);
                if (results.length > 0) {
                    resultOutput.innerHTML = `<pre><code>${JSON.stringify(results, null, 2)}</code></pre>`;

                    hljs.highlightElement(resultOutput.querySelector('code'));
                } else {
                    resultOutput.textContent = '未找到匹配的属性。';
                }
            } catch (e) {
                errorMessage.textContent = `提取过程中发生错误: ${e.message}`;
                errorMessage.style.display = 'block';
            }
        });

        // 事件监听器：重置
        resetBtn.addEventListener('click', () => {
            jsonInput.value = '';
            selectorInput.value = '';
            resultOutput.textContent = '';
            errorMessage.style.display = 'none';
        });

        // 初始示例数据
        jsonInput.value = JSON.stringify({
            user: { profile: { name: 'Alice', age: 30 } },
            products: [
                { id: 1, name: 'Laptop', price: 1200 },
                { id: 2, name: 'Mouse', price: 25 }
            ],
            settings: { theme: 'dark' }
        }, null, 2);
        selectorInput.value = 'user.profile.name, products[0].name, settings.theme, products[1].price, user.profile.age';

        // 页面加载时自动执行一次提取
        generateBtn.click();
    </script>
</body>

</html>